package com.sam.im.heiqun.utils;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.sam.im.heiqun.uis.activities.MainActivity;

import me.leolin.shortcutbadger.ShortcutBadger;

//引用到的工具类
import android.os.Environment;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

/**
 * Created by wangguodong on 2018/1/2.
 */

public class BadgeTool {

    private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code";
    private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";

    public static boolean showCount(Activity activity, int badgeCount, int bagCount){
        try {
            String manufacturer = android.os.Build.MANUFACTURER;
//            String model = android.os.Build.MODEL;

            Log.i("xiaomi", "showCount: ====================================manufacturer="+manufacturer);
//            Log.i("xiaomi", "showCount: ====================================model="+model);

            BuildProperties buildProperties = new BuildProperties();

            String name = buildProperties.getProperty(KEY_MIUI_VERSION_NAME, null);
//            String codemi = buildProperties.getProperty(KEY_MIUI_VERSION_CODE, null);
            Log.i("xiaomi", "showCount: ====================================name="+name);
//            Log.i("xiaomi", "showCount: ====================================codemi="+codemi);
            if (null!=manufacturer && "Xiaomi".equals(manufacturer)){
                if (null!=name && !TextUtils.isEmpty(name)){
                    if (name.startsWith("v") || name.startsWith("V")){
                        String code = name.substring(1);
                        if (Integer.parseInt(code) >= 6){
                            activity.startService(
                                    new Intent(activity, BadgeIntentService.class).putExtra("badgeCount", badgeCount)
                            );
                            return true;
                        }
                    }
                }
            }

            return ShortcutBadger.applyCount(activity, badgeCount);

        }catch (Exception e){
            e.printStackTrace();
        }
        return false ;
    }

     static class BuildProperties {

        private final Properties properties;

        private BuildProperties() throws IOException {
            properties = new Properties();
            properties.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop")));
        }

        public boolean containsKey(final Object key) {
            return properties.containsKey(key);
        }

        public boolean containsValue(final Object value) {
            return properties.containsValue(value);
        }

        public Set<Entry<Object, Object>> entrySet() {
            return properties.entrySet();
        }

        public String getProperty(final String name) {
            return properties.getProperty(name);
        }

        public String getProperty(final String name, final String defaultValue) {
            return properties.getProperty(name, defaultValue);
        }

        public boolean isEmpty() {
            return properties.isEmpty();
        }

        public Enumeration<Object> keys() {
            return properties.keys();
        }

        public Set<Object> keySet() {
            return properties.keySet();
        }

        public int size() {
            return properties.size();
        }

        public Collection<Object> values() {
            return properties.values();
        }

//         static BuildProperties newInstance() throws IOException {
//            return new BuildProperties();
//        }

    }

}
